const logger = require('../utils').getLogger(__filename);
const redisClient = require('../redis/redisClient');
const Mix = require('./Mix');
const Individual = require('./Individual');

class EndWithMeetingId {
	constructor(meetingId) {
		this.meetingId = meetingId
	}

	async end() {
		let records = await redisClient.getAllRecords();
		let meetings = records.filter(r => r.indexOf(`:${this.meetingId}:`) >= 0)
		let all = []
		logger.info(`end meeting:${this.meetingId}=>keys:${JSON.stringify(meetings)}`)
		return Promise.all(meetings.reduce((ret, key) => {
			ret.push(redisClient.getMeetingInfo(key).then(info => {
				let controller
				let events = JSON.parse(info.events)
				delete info.events
				switch (info.modal) {
					case 'mix':
						controller = new Mix(info, events)
						break;
					default:
						controller = new Individual(info, events);
				}
				return controller['stop']().then(() => {
					redisClient.deleteUID(this.meetingId);
				});
			}))
			return ret;
		}, []))
	}
}

module.exports = EndWithMeetingId
